package com.pai.dao.role;

import com.pai.dao.BaseDao;
import com.pai.entity.AppRole;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements  RoleDao{
    /**
     * 获取列表
     * @param connection
     * @return
     * @throws Exception
     */
    @Override
    public List<AppRole> getList(Connection connection) throws Exception {
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        List<AppRole> roleList = new ArrayList<AppRole>();

        StringBuffer sql = new StringBuffer();
        //没有查询条件限制下的原版查询语句
        sql.append("select * from approle");
        List<Object> params = new ArrayList<Object>();


        System.out.println(sql);
        rs = BaseDao.execute(connection, preparedStatement, rs, sql.toString(), params.toArray());

        while (rs.next()){
            AppRole _role = new AppRole();
            _role.setId(rs.getInt("id"));
            _role.setRoleName(rs.getString("roleName"));
            _role.setDescription(rs.getString("description"));
            roleList.add(_role);
        }
        BaseDao.closeResource(null,preparedStatement,rs);
        return roleList;
    }



    /**
     * 获取角色详情
     * @param connection
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public AppRole getItemDetail(Connection connection, String id) throws Exception {
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        AppRole roleItem = null;

        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<Object>();
        //没有查询条件限制下的原版查询语句
        sql.append("select * from approle where id = ?");
        params.add(id);

        System.out.println(sql);
        rs = BaseDao.execute(connection, preparedStatement, rs, sql.toString(), params.toArray());

        while (rs.next()){
            AppRole _role = new AppRole();
            _role.setId(rs.getInt("id"));
            _role.setRoleName(rs.getString("roleName"));
            _role.setDescription(rs.getString("description"));
            roleItem = _role;
        }
        BaseDao.closeResource(null, preparedStatement, rs);
        return roleItem;
    }

    /**
     * 根据角色名称获取角色详情
     * @param connection
     * @param roleName  角色名称
     * @return
     * @throws Exception
     */
    @Override
    public AppRole getItemByName(Connection connection, String roleName) throws Exception {
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        AppRole roleItem = null;

        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<Object>();
        //没有查询条件限制下的原版查询语句
        sql.append("select * from approle where roleName = ?");
        params.add(roleName);

        System.out.println(sql);
        rs = BaseDao.execute(connection, preparedStatement, rs, sql.toString(), params.toArray());

        while (rs.next()){
            AppRole _role = new AppRole();
            _role.setId(rs.getInt("id"));
            _role.setRoleName(rs.getString("roleName"));
            _role.setDescription(rs.getString("description"));
            roleItem = _role;
        }
        BaseDao.closeResource(null, preparedStatement, rs);
        return roleItem;
    }
    /**
     * 新增角色
     * @param connection
     * @param role
     * @return
     * @throws Exception
     */
    @Override
    public int add(Connection connection, AppRole role) throws Exception {
        PreparedStatement preparedStatement = null;
        int rs = 0;

        String  sql = "insert into approle (roleName, description) values (?,?)";
        Object[] pramas = {role.getRoleName(), role.getDescription()};

        System.out.println(sql);
        try {
            rs = BaseDao.execute(connection, preparedStatement, sql, pramas);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        BaseDao.closeResource(null, preparedStatement, null);
        return rs;
    }



    /**
     * 更新角色
     * @param connection
     * @param role
     * @return
     * @throws Exception
     */
    @Override
    public int update(Connection connection, AppRole role) throws Exception {
        PreparedStatement preparedStatement = null;
        int rs = 0;

        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<Object>();

        sql.append("update approle set roleName = ?, description = ? where id = ?");
        params.add(role.getRoleName());
        params.add(role.getDescription());
        params.add(role.getId());

        System.out.println(sql);
        rs = BaseDao.execute(connection, preparedStatement, sql.toString(), params.toArray());

        BaseDao.closeResource(null, preparedStatement, null);
        return rs;
    }

    /**
     * 删除角色
     * @param connection
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public int delete(Connection connection, String id) throws Exception {
        PreparedStatement preparedStatement = null;
        int rs = 0;

        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<Object>();

        sql.append("delete from approle where id = ?");
        params.add(id);


        System.out.println(sql);
        rs = BaseDao.execute(connection, preparedStatement, sql.toString(), params.toArray());

        BaseDao.closeResource(null, preparedStatement, null);
        return rs;
    }
}
